package com.fany.utils;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by FANY on 2015/2/7.
 */
public class ConfigUtils extends Activity
{
    public static final String ConfigName = "fastsearcherconfig";
    private SharedPreferences _preferences;
    SharedPreferences.Editor _editor;
    private ArrayList<String> _searchPaths;
    private boolean _needReloadCache;

    private static ConfigUtils _instance;

    public static ConfigUtils getInstance()
    {
        if (_instance == null)
        {
            _instance = new ConfigUtils();
        }
        return _instance;
    }

    private ConfigUtils()
    {
        _preferences = ContextUtils.getInstance().getSharedPreferences(ConfigName, Context.MODE_PRIVATE);
        _editor = _preferences.edit();
        _searchPaths = new ArrayList<String>();
    }

    public void initConfiguration()
    {
        int size = _preferences.getInt("search_path_count", 0);
        if (size == 0)
        {
            loadDefaultSearchPaths();
        }
        else
        {
            _searchPaths.clear();
            for (int i = 0; i < size; i++)
            {
                _searchPaths.add(_preferences.getString("search_path_" + i, ""));
            }
        }

        _needReloadCache = _preferences.getBoolean("need_reload_cache", false);
    }

    private void loadDefaultSearchPaths()
    {
        File file = new File("/mnt");
        if (file.exists())
        {
            for (File child : file.listFiles())
            {
                if (child.getName().contains("sdcard"))
                {
                    addSearchPaths(child.getAbsolutePath());
                }
            }
        }
        else
        {
            file = Environment.getExternalStorageDirectory();
            for (File child : file.listFiles())
            {
                if (!child.getName().equals("emulated"))
                {
                    addSearchPaths(child.getAbsolutePath());
                }
            }
        }
    }

    public ArrayList<String> getSearchPaths()
    {
        return _searchPaths;
    }

    public void addSearchPaths(String path)
    {
        _searchPaths.add(path);
        SharedPreferences.Editor editor = _preferences.edit();
        editor.putInt("search_path_count", _searchPaths.size());
        for (int i = 0; i < _searchPaths.size(); i++)
        {
            editor.remove("search_path_" + i);
            editor.putString("search_path_" + i, _searchPaths.get(i));
        }
        editor.commit();
        setNeedReloadCache(true);
    }

    public void removeSearchPath(String path)
    {
        _searchPaths.remove(path);
        SharedPreferences.Editor editor = _preferences.edit();
        editor.remove("search_path_count");
        editor.putInt("search_path_count", _searchPaths.size());
        for (int i = 0; i <= _searchPaths.size(); i++)
        {
            editor.remove("search_path_" + i);
        }
        editor.commit();
        for (int i = 0; i < _searchPaths.size(); i++)
        {
            editor.putString("search_path_" + i, _searchPaths.get(i));
        }
        editor.commit();
        setNeedReloadCache(true);
    }

    public void resetSearchPath()
    {
        SharedPreferences.Editor editor = _preferences.edit();
        for (int i = 0; i < _searchPaths.size(); i++)
        {
            editor.remove("search_path_" + i);
        }
        editor.commit();
        _searchPaths.clear();
        loadDefaultSearchPaths();
        editor.commit();
    }

    public void setNeedReloadCache(boolean value)
    {
        _needReloadCache = value;
        _editor.putBoolean("need_reload_cache", _needReloadCache);
        _editor.commit();
    }

    public boolean getNeedReloadCache()
    {
        if (_needReloadCache)
        {
            return true;
        }
        if (getUseTimedRefresh())
        {
            final long OneDay = 24 * 60 * 60;
            if (getLastReloadCacheTime().getTime() / 1000 + OneDay * getReloadCacheDaysSpan() < (new Date()).getTime() / 1000)
            {
                return true;
            }
        }
        return false;
    }

    public int getReloadCacheDaysSpan()
    {
        return _preferences.getInt("reload_cache_days_span", 1);
    }

    public void setReloadCacheDaysSpan(int value)
    {
        _editor.putInt("reload_cache_days_span", value);
        _editor.commit();
    }

    public Date getLastReloadCacheTime()
    {
        long value = _preferences.getLong("last_reload_cache_time", (new Date()).getTime());
        return new Date(value);
    }

    public void setLastReloadCacheTime(Date date)
    {
        _editor.putLong("last_reload_cache_time", date.getTime());
        _editor.commit();
    }

    public int getCacheCapacity()
    {
        return _preferences.getInt("cache_capacity", 30000);
    }

    public void setCacheCapacity(int value)
    {
        _editor.putInt("cache_capacity", value);
        _editor.commit();
    }

    public boolean getUseTimedRefresh()
    {
        return _preferences.getBoolean("use_timed_refresh", true);
    }

    public void setUseTimedRefresh(boolean value)
    {
        _editor.putBoolean("use_timed_refresh", value);
        _editor.commit();
    }

    public boolean getUseCustomPath()
    {
        return _preferences.getBoolean("use_custom_path", true);
    }

    public void setUseCustomPath(boolean value)
    {
        _editor.putBoolean("use_custom_path", value);
        if (!value)
        {
            resetSearchPath();
        }
        _editor.commit();
    }
}
